package com.foreverwin.jzymes.report.task.service.impl;

import com.foreverwin.jzymes.common.domain.NcData;
import com.foreverwin.jzymes.common.service.INCCodeAdviceService;
import com.foreverwin.jzymes.common.service.IResourceAdviceService;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.qc.check.domain.QcCheck;
import com.foreverwin.jzymes.qc.check.domain.QcCheckFaiFail;
import com.foreverwin.jzymes.report.task.mapper.TaskReportMapper;
import com.foreverwin.jzymes.report.task.service.ITaskReportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Date;

@Service
public class TaskReportServiceImpl implements ITaskReportService {
    @Autowired
    TaskReportMapper taskReportMapper;
    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    INCCodeAdviceService ncCodeAdviceService;
    /**
     * 目检、三次元任务报表
     *
     * @param jsonMap
     * @return
     *
     */
    public List<QcCheck> findQcCheckByReport(Map< String,Object > jsonMap){
       Date startDate = null;
       Date endDate = null;
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

       if ( StringUtils.isBlank( (String)jsonMap.get("startDate") )){
           try {
               startDate = simpleDateFormat.parse((String)jsonMap.get("startDate"));
           } catch (ParseException e) {
               e.printStackTrace();
           }
       }

       if ( !StringUtils.isBlank( (String)jsonMap.get("endDate") )){
           try {
               endDate = simpleDateFormat.parse((String)jsonMap.get("endDate"));
           } catch (ParseException e) {
               e.printStackTrace();
           }
       }

     /**检查CNC设备*/
     if ( !StringUtils.isBlank( (String)jsonMap.get("resrce") ) ){
         resourceAdviceService.existResource( (String)jsonMap.get("site"),(String)jsonMap.get("resrce") );
     }

     List<QcCheck> qcChecks =taskReportMapper.findQcCheckByReport( (String)jsonMap.get("site"),(String)jsonMap.get("resrce"),startDate,endDate );

     return qcChecks;
    }

    /**
     * 目检、三次元不良项目
     *
     * @param jsonMap
     * @return
     *
     */
    public List<NcData> findtaskNcData(Map<String,Object> jsonMap ){
        String site = (String)jsonMap.get("site");
        String operation = resourceAdviceService.findOperationByResource( site ,(String)jsonMap.get("resrce"));

        List<NcData> ncData = taskReportMapper.findtaskNcData( operation ,site,(String)jsonMap.get("resrce"),(String)jsonMap.get("carrier") );

        for ( NcData _ncData : ncData){
           String ncCode =  _ncData.getNcCode();
           Map<String,Object> map = ncCodeAdviceService.existNCCode( site , ncCode );
           _ncData.setDateCode( (String)map.get("DESCRIPTION") );
        }

        return ncData;
    }

    /**
     * 三次元测量值
     *
     * @param
     * @return
     *
     */
    public List<QcCheckFaiFail> findtaskFail(Date createdDateTime){

        List<QcCheckFaiFail> qcCheckFaiFails = taskReportMapper.findtaskFail( createdDateTime );

        return qcCheckFaiFails;
    }
}
